package com.lwlean.test.aliyun;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * Created by lean on 2016/12/19.
 */
@Component
public class OSSUnit {

    private static final Logger log = LoggerFactory.getLogger(OSSUnit.class);


    @Value("${endpoint}")
    private String endpoint = "oss-cn-hongkong.aliyuncs.com";

    @Value("${accessKeyId}")
    private String accessKeyId = "LTAIG4Z4FyUGgh3C";

    @Value("${accessKeySecret}")
    private String accessKeySecret = "HvBJlR9mbAGulFAtQeKkmAsIj6pdxl";

    public OSSUnit() {
        super();
    }

    /**
     * 创建阿里云OSS客户端对象
     * @return
     */
    public OSSClient getOSSClient(){
        return new OSSClient(endpoint,accessKeyId,accessKeySecret);
    }

    /**
     * 创建bucket
     * @param ossClient
     * @param bucketName
     * @return
     */
    public boolean createBucket(OSSClient ossClient, String bucketName){
        final Bucket bucket = ossClient.createBucket(bucketName);
        return bucketName.equals(bucket.getName());
    }

    /**
     * 上传Object到oss上
     * @param ossClient
     * @param file
     * @param bucketName
     * @param diskName
     * @return
     */
    public String uploadObject2OSS(OSSClient ossClient, File file, String bucketName, String diskName){
        String resultString = null;
        try {
            InputStream is = new FileInputStream(file);
            final String fileName = file.getName();
            final long fileSize  = file.length();
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentLength(is.available());
            metadata.setCacheControl("no-cache");
            metadata.setHeader("Pragma","no-cache");
            metadata.setContentEncoding("UTF-8");
//            metadata.setContentType(getContentType(fileName));
            metadata.setContentType("application/octst-stream");
            metadata.setContentDisposition("filename/filesize=" + fileName + "/" + fileSize + "Byte.");
            final PutObjectResult putObjectResult = ossClient.putObject(bucketName, diskName + fileName, is, metadata);
            resultString = putObjectResult.getETag();

        } catch (Exception e) {
            log.error("upload faild in case :{}",e,e.getMessage());
        }

        return resultString;
    }




    /**
     * 通过文件名判断并获取OSS服务文件上传时文件的contentType
     * @param fileName 文件名
     * @return 文件的contentType
     */
    public static final String getContentType(String fileName){
        String fileExtension = fileName.substring(fileName.lastIndexOf("."));
        if("bmp".equalsIgnoreCase(fileExtension)) return "image/bmp";
        if("gif".equalsIgnoreCase(fileExtension)) return "image/gif";
        if("jpeg".equalsIgnoreCase(fileExtension) || "jpg".equalsIgnoreCase(fileExtension)  || "png".equalsIgnoreCase(fileExtension) ) return "image/jpeg";
        if("html".equalsIgnoreCase(fileExtension)) return "text/html";
        if("txt".equalsIgnoreCase(fileExtension)) return "text/plain";
        if("vsd".equalsIgnoreCase(fileExtension)) return "application/vnd.visio";
        if("ppt".equalsIgnoreCase(fileExtension) || "pptx".equalsIgnoreCase(fileExtension)) return "application/vnd.ms-powerpoint";
        if("doc".equalsIgnoreCase(fileExtension) || "docx".equalsIgnoreCase(fileExtension)) return "application/msword";
        if("xml".equalsIgnoreCase(fileExtension)) return "text/xml";
        if("mp3".equalsIgnoreCase(fileExtension)) return "audio/mp3";
        return "text/html";
    }

}
